package com.mark.web.bill.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.mark.common.Constant;
import com.mark.common.ServerResponse;
import com.mark.common.utils.OSSClientUtil;
import com.mark.web.bill.entity.dto.GoodsDetail;
import com.mark.web.bill.entity.po.Goods;
import com.mark.web.bill.entity.po.GoodsImg;
import com.mark.web.bill.entity.vo.GoodsDetailVo;
import com.mark.web.bill.enums.GoodsEnum;
import com.mark.web.bill.mapper.GoodsImgMapper;
import com.mark.web.bill.mapper.GoodsMapper;
import com.mark.web.bill.service.IGoodsImgService;
import com.mark.web.bill.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * @Description: 商品加工零件的业务层接口实现
 * @Author: Mark
 * @CreateDate: 2019/2/2 22:51
 * @Version: 2.0
 * @Copyright : 豆浆油条个人非正式工作室
 */
@Service
public class GoodsServiceImpl implements IGoodsService {
    private final GoodsImgMapper goodsImgMapper;
    private final GoodsMapper goodsMapper;
    private final IGoodsImgService iGoodsImgService;

    @Autowired
    public GoodsServiceImpl(GoodsImgMapper goodsImgMapper, GoodsMapper goodsMapper, IGoodsImgService iGoodsImgService) {
        this.goodsImgMapper = goodsImgMapper;
        this.goodsMapper = goodsMapper;
        this.iGoodsImgService = iGoodsImgService;
    }


    //region 添加一条加工零件记录
    @Override
    public ServerResponse<String> add(Goods goods, MultipartFile goodsImage) {
        //账单的状态默认为可用
        goods.setEnableStatus(GoodsEnum.AVAILABLE.getState());
        int affectNum = goodsMapper.insert(goods);
        if (affectNum != Constant.SUCCESS) {
            ServerResponse.createByErrorMessage("保存信息失败！");
        }
        if(goodsImage != null){
            //上传图片到OSS服务器
            String url = OSSClientUtil.uploadImg2Oss(goodsImage, "Goods", goods.getId());
            //保存加工零件的图片数据到DB
            iGoodsImgService.add(url,goods.getId());
        }
        return ServerResponse.createBySuccessMessage("添加加工零件成功！");
    }
    //endregion

    @Override
    public ServerResponse<List<Map<String, Object>>> getOptionInfo(Integer categoryId) {
        List<Map<String, Object>> result = goodsMapper.selectAllGoodsByCate(categoryId);
        return ServerResponse.createBySuccess(result);
    }

    //region 分页获取加工商品信息
    @Override
    public ServerResponse<PageInfo<GoodsDetailVo>> getGoodsList(int pageNum, int pageSize,
                                                                int currentUserId, Integer categoryId,String searchGoodsName) {
        PageHelper.startPage(pageNum, pageSize);
        List<GoodsDetail> goodsDetailList = goodsMapper.searchGoodsDetailList(categoryId, currentUserId,searchGoodsName);
        List<GoodsDetailVo> goodsDetailVoList = Lists.newArrayList();
        for(GoodsDetail goodsDetail : goodsDetailList){
            GoodsDetailVo goodsDetailVo = new GoodsDetailVo(goodsDetail);
            goodsDetailVoList.add(goodsDetailVo);
        }
        PageInfo<GoodsDetailVo> result = new PageInfo<>(goodsDetailVoList);
        result.setTotal(new PageInfo<>(goodsDetailList).getTotal());
        return ServerResponse.createBySuccess(result);
    }
    //endregion

    //region 编辑一条加工零件记录
    @Override
    public ServerResponse<String> edit(Goods goods, MultipartFile goodsImage) {
        int update = goodsMapper.updateByPrimaryKeySelective(goods);
        if(update != Constant.SUCCESS){
            return ServerResponse.createBySuccessMessage("更新加工零件失败！");
        }
        if(goodsImage != null){
            //上传图片到OSS服务器
            String url = OSSClientUtil.uploadImg2Oss(goodsImage, "Goods", goods.getId());

            //检查原来的信息是够已经存在
            GoodsImg goodsImg = goodsImgMapper.selectByFkGoodsId(goods.getId());
            if(goodsImg == null){

                //保存加工零件的图片数据到DB
                iGoodsImgService.add(url,goods.getId());
               //原本不存在图片
            }else {
                //原本有图片则更新图片
                // TODO: 2019/2/23
                //删除原来的图片

                //保存加工零件的图片数据到DB
                iGoodsImgService.update(url,goods.getId());
            }
        }
        return ServerResponse.createBySuccessMessage("更新加工零件成功！");
    }
    //endregion

}
